
#ifndef P2PCONNECTION_H
#define P2PCONNECTION_H

#include <vector>
#include <wx/event.h>

#include "tux.h"

#include "MsnConnection.h"

class MsnSession;

class P2PConnection : public wxThread
{
protected:
	enum WorkMode{
		CLIENT = 0,
		SERVER
	};

protected:
	MsnSession* session;

	wxString sessionID;

	wxString callID;

	bool userCancelled;
	bool byeReceived;

	wxSocketBase* socket;

	wxSocketServer* serverSocket;

	WorkMode workMode;

public:
    P2PConnection(MsnSession* _session, const wxString& sid);

	virtual ~P2PConnection();

	void Start();

	int P2PConnection::OpenServerSocket(int port);

	bool ConnectSocket(const wxString& hostname,int port);

	bool AcceptSocket();

	void Cancel();

	void Close();

	void SendChunk(char* chunk, unsigned int size);

	void ReadChunk(char* chunk, unsigned int size);


	bool TestSocketReadable();


	void SetCallID(const wxString& id){callID = id;}

protected:
	void SendHandShake();
	void ReceiveHandShake();

	void SendP2PACKMessage(P2PMessage* msg);
	
	void SendP2PByeMessage();

};


enum 
{
	SOCKET_ID = 1000
};

class TestClass
{
public:
	wxString testString222;

};

class P2PFileSender : public P2PConnection, public wxEvtHandler
{
private:
	P2PMessage* message;

	//std::vector<int> testVector;
	
	TestClass testClass;


	wxMutex testMutex;

public:
    P2PFileSender(MsnSession* _session, const wxString& sid);
	virtual ~P2PFileSender(){};

	
	
	virtual void* Entry();

private:
	void OnSocketEvent(wxSocketEvent& event);
	
	void RegisterSocketEvent();

	void HandleInputEvent();

DECLARE_EVENT_TABLE()

};


class P2PFileReceiver : public P2PConnection
{
public:
    P2PFileReceiver(MsnSession* _session, const wxString& sid);
	virtual ~P2PFileReceiver(){};
	
	virtual void* Entry();

};



#endif
